package com.utils;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import net.forsalemall.android.R;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class HttpUtils {
//	private static final boolean DEBUG_ENABLED = false;
	private static final boolean DEBUG_ENABLED = true;
	private static String deviceDetails;
	private static String forsaleSuperCategoriesData;
	private static String serviceSuperCategoriesData;
	private static String dealCategoriesData;

	// ////
	// private static final String SERVER_URL =
	// "http://topofstacksoftware.com/4sale";
	private static final String SERVER_URL = "http://4salemall.com/4sale";

	private static final String PROCEDURE_PAGE = SERVER_URL + "/sp.php";
	private static final String FILE_UPLOAD_URL = SERVER_URL + "/api.php";

	public static final int HTTP_TIMEOUT = 30 * 1000;

	public static synchronized final byte[] getHttpResponse(String url)
			throws IOException {
		HttpClient client = getHttpClient();
		HttpGet request = new HttpGet(url);
		HttpResponse response = client.execute(request);
		return EntityUtils.toByteArray(response.getEntity());
	}

	public static final String uploadImageFile(File file) throws Exception {
		String ret = null;
			HttpURLConnection connection = null;
			DataOutputStream outputStream = null;
			String lineEnd = "\r\n";
			String twoHyphens = "--";
			String boundary = "*****";

			URL url = new URL(FILE_UPLOAD_URL);
			connection = (HttpURLConnection) url.openConnection();
			// Allow Inputs & Outputs
			connection.setDoInput(true);
			connection.setDoOutput(true);
			connection.setUseCaches(false);
			// Enable POST method
			connection.setRequestMethod("POST");
			connection.setRequestProperty("Connection", "Keep-Alive");
			connection.setRequestProperty("Content-Type",
					"multipart/form-data;boundary=" + boundary);
			outputStream = new DataOutputStream(connection.getOutputStream());
			outputStream.writeBytes(twoHyphens + boundary + lineEnd);
			outputStream
					.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
							+ file.getPath() + "\"" + lineEnd);
			outputStream.writeBytes(lineEnd);
			int bytesRead;
			byte[] buffer = new byte[2048];

			if (file.length() > 512000) {
				outputStream.write(Utils.getImageData(file,
						Constants.IMAGE_DIMENSION));
			} else {
				FileInputStream fileInputStream = new FileInputStream(file);
				while ((bytesRead = fileInputStream.read(buffer)) != -1) {
					outputStream.write(buffer, 0, bytesRead);
				}
				fileInputStream.close();
			}
			outputStream.writeBytes(lineEnd);
			outputStream.writeBytes(twoHyphens + boundary + twoHyphens
					+ lineEnd);
			// Responses from the server (code and message)
			int serverResponseCode = connection.getResponseCode();
			String serverResponseMessage = connection.getResponseMessage();
			outputStream.flush();
			outputStream.close();
			InputStream is = connection.getInputStream();
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int read;
			while ((read = is.read(buffer)) != -1) {
				baos.write(buffer, 0, read);
			}
			buffer = baos.toByteArray();
			ret = new String(buffer);
			connection.disconnect();
		if (DEBUG_ENABLED) {
			Log.i("DREG", "FILE_UPLOAD_RESP=" + ret);
		}
		return ret;
	}

	public static final Bitmap downloadFile(String url, Context context) {
		Bitmap ret = null;
		try {
			URL u = new URL(url.replace(" ", "%20"));
			HttpURLConnection c = (HttpURLConnection) u.openConnection();
			c.setConnectTimeout(5000);
			c.setRequestMethod("GET");
			c.connect();
			if (c.getResponseCode() == HttpURLConnection.HTTP_OK) {
				ret = BitmapFactory.decodeStream(c.getInputStream());
				c.disconnect();
			}

		} catch (Exception e) {
			ret = BitmapFactory.decodeResource(context.getResources(),
					R.drawable.home_logo);
			e.printStackTrace();
		}
		return ret;
	}

	public static final String getFileUrl(String dir, String fileName) {
		return SERVER_URL + "/" + dir + "/" + fileName;
	}

	public static final String getForsaleCategoriesData(String countryCode)
			throws Exception {
		if (forsaleSuperCategoriesData == null) {
			JSONObject jo = new JSONObject();
			JSONArray ja = new JSONArray();
			jo.put("pn", "get_sup_categories_with_ad_count");
			JSONObject param = new JSONObject();
			param.put("o", 0);
			param.put("v", Constants.TYPE_NORMAL_CAT);
			ja.put(param);
			param = new JSONObject();
			param.put("o", 1);
			param.put("v", countryCode);
			ja.put(param);
			jo.put("params", ja);
			forsaleSuperCategoriesData = executeHttpPostAsString(
					PROCEDURE_PAGE, jo);
		}
		return forsaleSuperCategoriesData;
	}

	public static final String getServiceCategoriesData(String countryCode)
			throws Exception {
		if (serviceSuperCategoriesData == null) {
			JSONObject jo = new JSONObject();
			JSONArray ja = new JSONArray();
			jo.put("pn", "get_sup_categories_with_ad_count");
			JSONObject param = new JSONObject();
			param.put("o", 0);
			param.put("v", Constants.TYPE_FEATURE_CAT);
			ja.put(param);
			param = new JSONObject();
			param.put("o", 1);
			param.put("v", countryCode);
			ja.put(param);
			jo.put("params", ja);
			serviceSuperCategoriesData = executeHttpPostAsString(
					PROCEDURE_PAGE, jo);
		}
		return serviceSuperCategoriesData;
	}

	public static final String getDealsData() throws Exception {
		if (dealCategoriesData == null) {
			JSONObject jo = new JSONObject();
			JSONArray ja = new JSONArray();
			jo.put("pn", "get_deals_cat");
			JSONObject param = new JSONObject();
			ja.put(param);
			jo.put("params", ja);
			dealCategoriesData = executeHttpPostAsString(PROCEDURE_PAGE, jo);
		}
		return dealCategoriesData;
	}

	public static final String getAdPostedCountInPeriod(String deviceId)
			throws Exception {
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "get_ad_posted_count");
		JSONObject param = new JSONObject();
		param.put("o", 0);
		param.put("v", deviceId);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 1);
		param.put("v", Constants.CYCLE_OF_POST_AD);
		ja.put(param);
		jo.put("params", ja);
		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
	}

	public static final String getSplashImagesData() throws Exception {
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "get_sp_imgs");
		jo.put("params", ja);
		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
	}

	public static final String getConstantsData() throws Exception {
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "get_constants");
		jo.put("params", ja);
		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
	}

	public static final String postAd(String deviceId, String text, int adType,
			int catId, String countryCode, String phoneNo, String fileNames)
			throws Exception {
		text = Utils.getEncodedString(text);
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "post_ad");
		JSONObject param = new JSONObject();
		param.put("o", 0);
		param.put("v", deviceId);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 1);
		param.put("v", text);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 2);
		param.put("v", adType);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 3);
		param.put("v", catId);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 4);
		param.put("v", countryCode);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 5);
		param.put("v", phoneNo);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 6);
		param.put("v", fileNames);
		ja.put(param);
		jo.put("params", ja);
		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
	}

	public static final String postInquiry(String deviceId, String phoneNo,
			String name, String email, String inquiry) throws Exception {
		name = Utils.getEncodedString(name);
		inquiry = Utils.getEncodedString(inquiry);
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "add_inquiry");
		JSONObject param = new JSONObject();
		param.put("o", 0);
		param.put("v", deviceId);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 1);
		param.put("v", phoneNo);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 2);
		param.put("v", name);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 3);
		param.put("v", email);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 4);
		param.put("v", inquiry);
		ja.put(param);
		jo.put("params", ja);
		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
	}

	public static final String attachFiles(int adId, String fileNames)
			throws Exception {
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "attach_files");
		JSONObject param = new JSONObject();
		param.put("o", 0);
		param.put("v", adId);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 1);
		param.put("v", fileNames);
		ja.put(param);
		jo.put("params", ja);
		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
	}

	public static final String updateAd(int adId, String text, String phoneNo,
			String fileNames) throws Exception {
		text = Utils.getEncodedString(text);
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "update_ad");
		JSONObject param = new JSONObject();
		param.put("o", 0);
		param.put("v", adId);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 1);
		param.put("v", text);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 2);
		param.put("v", phoneNo);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 3);
		param.put("v", fileNames);
		ja.put(param);
		jo.put("params", ja);
		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
	}

	public static final String markSold(int adId) throws Exception {
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "mark_sold");
		JSONObject param = new JSONObject();
		param.put("o", 0);
		param.put("v", adId);
		ja.put(param);
		jo.put("params", ja);
		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
	}

//	public static final String searchAds(int adType, int catId,
//			String countryCode) throws Exception {
//		JSONObject jo = new JSONObject();
//		JSONArray ja = new JSONArray();
//		jo.put("pn", "search_ads");
//		JSONObject param = new JSONObject();
//		param.put("o", 0);
//		param.put("v", adType);
//		ja.put(param);
//		param = new JSONObject();
//		param.put("o", 1);
//		param.put("v", catId);
//		ja.put(param);
//		param = new JSONObject();
//		param.put("o", 2);
//		param.put("v", countryCode);
//		ja.put(param);
//		jo.put("params", ja);
//		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
//	}
	public static final String limitSearchAds(int adType, int catId,
			String countryCode,int skip,int count) throws Exception {
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "limit_search_ads");
		JSONObject param = new JSONObject();
		param.put("o", 0);
		param.put("v", adType);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 1);
		param.put("v", catId);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 2);
		param.put("v", countryCode);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 3);
		param.put("v", skip);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 4);
		param.put("v", count);
		ja.put(param);
		jo.put("params", ja);
		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
	}

	public static final String getBanner(int catId, String countryCode)
			throws Exception {
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "get_banner");
		JSONObject param = new JSONObject();
		param = new JSONObject();
		param.put("o", 0);
		param.put("v", catId);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 1);
		param.put("v", countryCode);
		ja.put(param);
		jo.put("params", ja);
		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
	}
	
	public static final String getLimitFaveriteAds(String deviceId,
			String countryCode,int skip,int count) throws Exception {
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "limit_get_fav_ads");
		JSONObject param = new JSONObject();
		param.put("o", 0);
		param.put("v", deviceId);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 1);
		param.put("v", countryCode);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 2);
		param.put("v", skip);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 3);
		param.put("v", count);
		ja.put(param);
		jo.put("params", ja);
		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
	}

//	public static final String getFaveriteAds(String deviceId,
//			String countryCode) throws Exception {
//		JSONObject jo = new JSONObject();
//		JSONArray ja = new JSONArray();
//		jo.put("pn", "get_fav_ads");
//		JSONObject param = new JSONObject();
//		param.put("o", 0);
//		param.put("v", deviceId);
//		ja.put(param);
//		param = new JSONObject();
//		param.put("o", 1);
//		param.put("v", countryCode);
//		ja.put(param);
//		jo.put("params", ja);
//		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
//	}

//	public static final String getLatestAds(String countryCode)
//			throws Exception {
//		JSONObject jo = new JSONObject();
//		JSONArray ja = new JSONArray();
//		jo.put("pn", "latest_ads");
//		JSONObject param = new JSONObject();
//		param.put("o", 0);
//		param.put("v", countryCode);
//		ja.put(param);
//		jo.put("params", ja);
//		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
//	}
	
	public static final String getLimitLatestAds(String countryCode,int skip,int count)
			throws Exception {
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "limit_latest_ads");
		JSONObject param = new JSONObject();
		param.put("o", 0);
		param.put("v", countryCode);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 1);
		param.put("v", skip);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 2);
		param.put("v", count);
		ja.put(param);
		jo.put("params", ja);
		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
	}
	
	public static final String getLimitSearchSupCatAds(int adType,int supCatId, String countryCode,int skip,int count)
			throws Exception {
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "limit_search_super_cat_ads");
		JSONObject param = new JSONObject();
		param.put("o", 0);
		param.put("v", adType);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 1);
		param.put("v", supCatId);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 2);
		param.put("v", countryCode);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 3);
		param.put("v", skip);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 4);
		param.put("v", count);
		ja.put(param);
		jo.put("params", ja);
		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
	}

//	public static final String searchAds(String countryCode, String searchKey)
//			throws Exception {
//		JSONObject jo = new JSONObject();
//		JSONArray ja = new JSONArray();
//		jo.put("pn", "search_ads_by_key");
//		// etThreeProbabableEncodedKeys(final String key)
//		JSONObject param = new JSONObject();
//		param.put("o", 0);
//		param.put("v", countryCode);
//		ja.put(param);
//		param = new JSONObject();
//		param.put("o", 1);
//		param.put("v", Utils.getEncodedString(searchKey));
//		ja.put(param);
//		jo.put("params", ja);
//		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
//	}
	
	public static final String limitSearchAds(String countryCode, String searchKey,int skip,int count)
			throws Exception {
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "limit_search_ads_by_key");
		// etThreeProbabableEncodedKeys(final String key)
		JSONObject param = new JSONObject();
		param.put("o", 0);
		param.put("v", countryCode);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 1);
		param.put("v", Utils.getEncodedString(searchKey));
		ja.put(param);
		param = new JSONObject();
		param.put("o", 2);
		param.put("v", skip);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 3);
		param.put("v", count);
		ja.put(param);
		
		jo.put("params", ja);
		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
	}

//	public static final String getMyAds(String deviceId, String countryCode)
//			throws Exception {
//		JSONObject jo = new JSONObject();
//		JSONArray ja = new JSONArray();
//		jo.put("pn", "get_my_ads");
//		JSONObject param = new JSONObject();
//		param.put("o", 0);
//		param.put("v", deviceId);
//		ja.put(param);
//		param = new JSONObject();
//		param.put("o", 1);
//		param.put("v", countryCode);
//		ja.put(param);
//		jo.put("params", ja);
//		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
//	}
	
	public static final String getLimitMyAds(String deviceId, String countryCode,int skip,int count)
			throws Exception {
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "limit_get_my_ads");
		JSONObject param = new JSONObject();
		param.put("o", 0);
		param.put("v", deviceId);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 1);
		param.put("v", countryCode);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 2);
		param.put("v", skip);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 3);
		param.put("v", count);
		ja.put(param);
		jo.put("params", ja);
		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
	}

	public static final String getAdDetails(int adId, String deviceId)
			throws Exception {
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "get_ad_details");
		JSONObject param = new JSONObject();
		param.put("o", 0);
		param.put("v", adId);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 1);
		param.put("v", deviceId);
		ja.put(param);
		jo.put("params", ja);
		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
	}

	public static final String addToFavorite(String deviceId, String adIds)
			throws Exception {
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "add_to_favorites");
		JSONObject param = new JSONObject();
		param.put("o", 0);
		param.put("v", deviceId);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 1);
		param.put("v", adIds);
		ja.put(param);
		jo.put("params", ja);
		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
	}

	public static final String removeFromFavorite(String deviceId, String adIds)
			throws Exception {
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "remove_from_favorites");
		JSONObject param = new JSONObject();
		param.put("o", 0);
		param.put("v", deviceId);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 1);
		param.put("v", adIds);
		ja.put(param);
		jo.put("params", ja);
		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
	}

	public static final String deleteAds(String adIds) throws Exception {
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "delete_ads");
		JSONObject param = new JSONObject();
		param.put("o", 0);
		param.put("v", adIds);
		ja.put(param);
		jo.put("params", ja);
		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
	}

	public static final String getDeviceDetails(boolean forceRefresh,
			String deviceId) throws Exception {
		if ((deviceDetails == null) || forceRefresh) {
			JSONObject jo = new JSONObject();
			JSONArray ja = new JSONArray();
			jo.put("pn", "get_device_details");
			JSONObject param = new JSONObject();
			param.put("o", 0);
			param.put("v", deviceId);
			ja.put(param);
			param = new JSONObject();
			param.put("o", 1);
			param.put("v", Constants.DEVICE_TYPE_ANDROID);
			ja.put(param);
			jo.put("params", ja);
			deviceDetails = executeHttpPostAsString(PROCEDURE_PAGE, jo);
		}
		return deviceDetails;
	}

	public static final String registerDevice(String deviceId, String phoneNo)
			throws Exception {
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "register_device");
		JSONObject param = new JSONObject();
		param.put("o", 0);
		param.put("v", deviceId);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 1);
		param.put("v", phoneNo);
		ja.put(param);
		jo.put("params", ja);
		deviceDetails = executeHttpPostAsString(PROCEDURE_PAGE, jo);
		return deviceDetails;
	}

	// public static final String registerDeviceUrl(String deviceId, String
	// phoneNo)
	// throws Exception {
	// JSONObject jo = new JSONObject();
	// JSONArray ja = new JSONArray();
	// jo.put("pn", "r_d");
	// JSONObject param = new JSONObject();
	// param.put("o", 0);
	// param.put("v", Utils.toHexString(deviceId));
	// ja.put(param);
	// param = new JSONObject();
	// param.put("o", 1);
	// param.put("v", Utils.toHexString(phoneNo));
	// ja.put(param);
	// jo.put("params", ja);
	// return PROCEDURE_PAGE + "?data=" + jo.toString();
	// }

	public static final String unregisterDevice(String deviceId)
			throws Exception {
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "unregister_device");
		JSONObject param = new JSONObject();
		param.put("o", 0);
		param.put("v", deviceId);
		ja.put(param);
		jo.put("params", ja);
		deviceDetails = executeHttpPostAsString(PROCEDURE_PAGE, jo);
		return deviceDetails;
	}

	public static final String submitPushKey(String deviceId, String pushKey)
			throws Exception {
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		jo.put("pn", "set_pn_key");
		JSONObject param = new JSONObject();
		param.put("o", 0);
		param.put("v", deviceId);
		ja.put(param);
		param = new JSONObject();
		param.put("o", 1);
		param.put("v", pushKey);
		ja.put(param);
		jo.put("params", ja);
		return executeHttpPostAsString(PROCEDURE_PAGE, jo);
	}

	private static final String executeHttpPostAsString(String url,
			JSONObject jo) throws Exception {
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("data", jo.toString()));
		return executeHttpPostAsString(url, nameValuePairs);
	}

	private static synchronized String executeHttpPostAsString(String url,
			List<NameValuePair> postParameters) throws Exception {
		HttpClient client = getHttpClient();
		HttpPost request = new HttpPost(url);
		UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
				postParameters);
		request.setEntity(formEntity);
		HttpResponse response = client.execute(request);
		String result = EntityUtils.toString(response.getEntity());
		if (DEBUG_ENABLED) {
			String str = "";
			for (int i = 0; i < postParameters.size(); i++) {
				str = str + "&" + postParameters.get(i).getName() + "="
						+ postParameters.get(i).getValue();
			}
			Log.i("DREG", "PARAM=" + str);
			Log.i("DREG", "RESULT" + result);
		}
		return result;
	}

	private static HttpClient mHttpClient;

	private static HttpClient getHttpClient() {
		if (mHttpClient == null) {
			mHttpClient = new DefaultHttpClient();
			final HttpParams params = mHttpClient.getParams();
			HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
			HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
			ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
		}
		return mHttpClient;
	}

	public static String getHttpGetStringResponce(String url) {
		try {
			HttpClient client = getHttpClient();
			HttpGet request = new HttpGet(url);
			HttpResponse response = client.execute(request);
			return EntityUtils.toString(response.getEntity());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
